package com.thinkfit.jy.activity;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.bluetooth.Bluetooth;
import com.thinkfit.jy.fragment.BluetoothFragment;
import com.thinkfit.jy.fragment.ChooseBirthdayFragment;
import com.thinkfit.jy.fragment.ChooseGenderFragment;
import com.thinkfit.jy.fragment.ChooseHeightFragment;
import com.thinkfit.jy.fragment.ChooseWeightFragment;
import com.thinkfit.jy.fragment.ScreenProtectFragment;
import com.thinkfit.jy.fragment.SelectScreenProtectTimeFragment;
import com.thinkfit.jy.fragment.SwitchLanguageFragment;
import com.thinkfit.jy.fragment.SwitchRegionFragment;
import com.thinkfit.jy.fragment.TNJBluetoothSettingFragment;
import com.thinkfit.jy.fragment.ThemeModeFragment;
import com.thinkfit.jy.fragment.UpdateNickFragment;
import com.thinkfit.jy.fragment.WifiConnectFragment;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.SimpleSettingViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;
import com.thinkfit.jy.utils.Utils;

import java.util.HashMap;

/**
 * 设置二级界面
 */
public class SettingNewOtherActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View {

    int type;
    ConstraintLayout clMain;
    TextView tvTitle,tvArea,tvLanguage,tvUnit;
    FrameLayout fl;
    private ImageView wifiImag;
    public static final int LANGUAGE = 1;//语言设置
    public static final int REGION = 2;//地区
    public static final int DISPLAY = 3;//显示模式
    public static final int THEME = 4;//主题
    public static final int BLUETOOTH = 5;// 蓝牙
    public static final int NICKNAME = 6;// 昵称
    public static final int CHOOSE_GENDER = 7;// 性别
    public static final int BIRTHDAY = 8;// 生日
    public static final int HEIGHT = 9;// 身高
    public static final int WEIGHT = 10;// 体重
    public static final int SCREEN_SAVER = 11;// 屏保
    public static final int SCREEN_SAVER_TIME = 12;// 屏保时间
    public static final int WIFI = 13;// wifi



    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_new_other;
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        type = getIntent().getIntExtra(Constants.BUNDLE_EXTRA,0);
        setViewsOnClickListener(R.id.ll_sport_back,R.id.cl_set_item);
        wifiImag = findViewById(R.id.iv_wifi);
        clMain = findViewById(R.id.cl_main);
        tvTitle = findViewById(R.id.tv_title);
        tvArea = findViewById(R.id.tv_area);
        tvLanguage = findViewById(R.id.tv_language);
        tvUnit = findViewById(R.id.tv_unit);

        fl = findViewById(R.id.fl);
        initType();
        Utils.initRegion(tvArea,tvLanguage,tvUnit);
    }


    public void initType(){
        if(type == LANGUAGE){
            tvTitle.setText(getString(R.string.app_switch_language));
            toFragment(new SwitchLanguageFragment());
        }else if(type == REGION){
            tvTitle.setText(getString(R.string.app_region));
            toFragment(new SwitchRegionFragment());
        }else if(type == DISPLAY){
            tvTitle.setText(getString(R.string.app_display_mode));
        }else if(type == THEME){
            tvTitle.setText(getString(R.string.app_wallpaper));
            toFragment(new ThemeModeFragment());
        }else if(type == BLUETOOTH){
            tvTitle.setText(getString(R.string.app_bluetooth));
//            toFragment(new BluetoothFragment());
            toFragment(new TNJBluetoothSettingFragment());
        }else if(type == NICKNAME){
            tvTitle.setText(getString(R.string.app_nick));
            toFragment(new UpdateNickFragment());
        }else if(type == CHOOSE_GENDER){
            tvTitle.setText(getString(R.string.app_sex));
            toFragment(new ChooseGenderFragment());
        }else if(type == BIRTHDAY){
            tvTitle.setText(getString(R.string.app_birthday));
            toFragment(new ChooseBirthdayFragment());
        }else if(type == HEIGHT){
            tvTitle.setText(getString(R.string.app_height));
            toFragment(new ChooseHeightFragment());
        }else if(type == WEIGHT){
            tvTitle.setText(getString(R.string.app_weight));
            toFragment(new ChooseWeightFragment());
        }else if(type == SCREEN_SAVER){
            tvTitle.setText(getString(R.string.app_screen_protect));
            toFragment(new ScreenProtectFragment());
        }else if(type == SCREEN_SAVER_TIME){
            tvTitle.setText(getString(R.string.app_screen_protect));
            toFragment(new SelectScreenProtectTimeFragment());
        }else if(type == WIFI){
            tvTitle.setText(getString(R.string.app_network));
            toFragment(new WifiConnectFragment());
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        showBottomUI();
        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
        initSound();
    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    public void onReceive(@NonNull HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if (map.containsKey(EventBusEvent.BLUETOOTH_SUCCESS)) {
            finish();
        }if (map.containsKey(EventBusEvent.BLUETOOTH_CHANGE)) {
            showBottomUI();
        }else if(map.containsKey(EventBusEvent.REFRESH_ACTIVITY)){
            type = (int) map.get(EventBusEvent.REFRESH_ACTIVITY);
            initType();
        }else if(map.containsKey(EventBusEvent.VOICE_CHANGE)){
            int i = (int) map.get(EventBusEvent.VOICE_CHANGE);
            if(i <= 0){
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

            }else {
                setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
            }
        } else {
            super.onReceive(map);
        }
    }

    public void showBottomUI(){
        if(Bluetooth.getInstance().isBluetoothOpened()){
            setImage(R.id.iv_bluetooth, BluetoothOrder.getInstance().isConnect() ?
                    R.drawable.img_main_bluetooth_connect : R.drawable.img_main_bluetooth);
        }else {
            setImage(R.id.iv_bluetooth, R.drawable.img_main_bluetooth_back);
        }
    }

    public void setWifiImag(boolean isWifiConnected) {
        if (isWifiConnected){
            wifiImag.setImageResource(  R.drawable.img_main_wifi);
        }else {
            wifiImag.setImageResource(R.drawable.img_main_wifi_back);
        }
    }

    public void toFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fl, fragment);
        ft.addToBackStack(null);
        ft.commit();
    }

    public void initSound(){
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        if(currentVolume <= 0){
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_off);

        }else {
            setImage(R.id.iv_sound,R.drawable.img_simple_set_sound_max);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_sport_back:
                if(type == SCREEN_SAVER_TIME){
                    type = SCREEN_SAVER;
                    tvTitle.setText(getString(R.string.app_screen_protect));
                    toFragment(new ScreenProtectFragment());
//                    getSupportFragmentManager().popBackStack();
                }else {
                    finish();
                }

                break;
            case R.id.cl_set_item:
//                SimpleSettingViewUtils.getInstance().initView(getContext(),this);
                break;
        }
    }

}